<?php
/**
 * 功能方法类库集合文件
 */
/**
 * 判断环境类型编码输出
 * @param  [type] $Str [description]
 * @return [type]      [description]
 */
function print_f($Str, $sleep = null)
{
    if(php_sapi_name() == 'cli'){
        if(stripos(php_uname('s'), 'windows') !== false){
            echo iconv('utf-8', 'gb2312', $Str);
        } else {
            echo $Str;
        }
        is_numeric($sleep) && sleep($sleep);
    }else{
        echo $Str;
    }
    if(is_bool($sleep) && $sleep == true) exit;
}

/**
 * 文本转成数组
 * @param  [type] $Str [description]
 * @return [type]      [description]
 */
function txttoarray($name){
    if(!file_exists($name)||file_get_contents($name)==""){
        file_put_contents($name,"");die($name."不能为空");
    }
    $contents = file_get_contents($name);
    return array_unique(array_filter(explode('|',str_replace(array("\r\n", "\r", "\n"), "|",$contents))));
}
/**
 * 随机文章
 */
function getarticle($pid,$config,$title){
    $article = file_get_contents('http://dal.120open.com/Home/Api/randomarticle/?pid='.$pid);
    $article = json_decode($article, true);
    return postBodyReplace($article['body'],$config['hosType'],$title);
}
function postBodyReplace($body, $hosType, $title){
    
        global $hosRepKey;

        // 删除多于换行
        $body = str_ireplace(array("\r", "\n"), '', $body);

        // 循环 替换 医院标记  循环每一个 标记
        foreach ($hosRepKey[$hosType] as $tagName => $tagValue) {
            if(is_array($tagValue)) $tagValue = $tagValue[array_rand($tagValue)];
            $tagValue = trim($tagValue);
            $body = str_ireplace('{$'.$tagName.'}', $tagValue, $body);
        }

        // 把 技术标记 删除
        $body = str_ireplace('{$HospitalSkill}', '', $body);

        // 替换商务通标记
        $body = preg_replace('#{$HospitalSwt}#', '<a href="javascript:;" target="_self" onclick="BsSwt.Class.OpenSwt(\'\',\'2\');return false;" rel="nofollow">立即在线咨询</a>', $body);
        $body = preg_replace('#{$HospitalSwt\.([^}]+)}#', '<a href="javascript:;" target="_self" onclick="BsSwt.Class.OpenSwt(\'\',\'2\');return false;" rel="nofollow">$1</a>', $body);

        // 替换关键词标记
        $body = str_ireplace('{$PostKeyword}', $title, $body);

        // 强制加首行空格
        $body = strip_tags(str_ireplace(array('  ','<p>','</p>','<br/>'), array('','       ',"\n\n","\n\n"), $body));
        
        return $body;
}

    // csv转json
    function csv2json($csvFileName, $headContr = array() ,$isBody = false)
    {
        // 获取 CSV内容
        $csvBody = $isBody ? $csvFileName : file_get_contents($csvFileName);

        // 读取并转换编码
        $csvCharset = mb_detect_encoding($csvBody, array('ASCII','UTF-8','GB18030','GB2312','GBK'));

        // 判断并转换编码
        if($csvCharset != 'UTF-8'){
            $csvBody = mb_convert_encoding($csvBody, 'UTF-8', $csvCharset);
        }

        // 处理Csv内容
        $csvArr = explode("\n", $csvBody);
        $jsonArr = array();
        // 循环每一行记录
        foreach ($csvArr as $id => $value) {
            // 第一行为表头
            if($id == 0){
                $csvTabList = explode(',', trim($value));

                foreach ($csvTabList as $csvTabId => $csvTabName) {
                    // 对应表头拼音
                    foreach ($headContr as $headKey => $headValue) {
                        if(trim($csvTabName) == trim($headValue)){
                            $csvTabList[$csvTabId] = trim($headKey);
                        }
                    }
                }
            }else{
                // 空行跳过
                if(empty(trim($value))) continue;
                // 解析单条记录字段
                $arrTmp = explode(',', $value);
                $arrInfo = array();
                // 循环每个字段对应的表头
                foreach ($arrTmp as $aid => $val) {
                    $arrInfo[$csvTabList[$aid]] = trim($val);
                }
                $jsonArr[] = $arrInfo;
            }
        }
        return $jsonArr;
    }
    
//获得title序号
function getTitleId($fileName){
    
    // 获取日志文件名
    $fileLogName = preg_replace('#\.([a-z0-9A-Z]+)$#', '_info.$1', $fileName);
    $fileLogStr  = file_exists($fileLogName) ? file_get_contents($fileLogName) : "x\n0";
    $fileLog     = explode("\n", $fileLogStr);

    // 判断关键词文件是否修改
    $keyFileHash = md5_file($fileName);
    $fileLogId = $keyFileHash == trim($fileLog['0']) ? intval($fileLog['1']) : 0;
    file_put_contents($fileLogName, "{$keyFileHash}\n{$fileLogId}");
    return $fileLogId;
}
//写入title序号
function setTitleId($fileName,$id){
    // 获取日志文件名
    $fileLogName = preg_replace('#\.([a-z0-9A-Z]+)$#', '_info.$1', $fileName);
    $fileLogStr  = file_exists($fileLogName) ? file_get_contents($fileLogName) : "x\n0";
    $fileLog     = explode("\n", $fileLogStr);
    
    // 判断关键词文件是否修改
    $keyFileHash = md5_file($fileName);
    $fileLogId = $keyFileHash == trim($fileLog['0']) ? $id : 0;
    file_put_contents($fileLogName, "{$keyFileHash}\n{$fileLogId}");
}


    function baidu_push($url)
    {
        // 提交地址
        $pushUrl = 'http://api.share.baidu.com/s.gif?l=' . $url;

        // 追加协议头
        $header = array(
            'Referer'    => $url,
            'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36'
        );

        
        // 初始化
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 65);
        curl_setopt($ch, CURLOPT_URL, $pushUrl);
        curl_setopt($ch, CURLOPT_USERAGENT, $header['User-Agent']);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        
        curl_exec($ch);
        $retHtml = curl_getinfo($ch);

        $code = false;
        
        if($retHtml["http_code"] == 200){
            $code = true;
        }
        return $code;
    }